package com.itheima.controller;

import com.itheima.bean.User;
import com.itheima.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/login")
 //  此处不能添加@ResponseBody,如果加了该注入，会直接将page.html当字符串返回前端
    //@ResponseBody
    public String save(String name, String password ,HttpServletRequest request/*, HttpServletResponse response*/)/* throws IOException, ServletException*/ {
        System.out.println(name);
        System.out.println(password);
        User user = userMapper.login(name, password);
        /*if (user != null) {
              重定向
            response.sendRedirect("/admin/manage.html");
        } else {
               请求转发
            request.getRequestDispatcher("/error.html").forward(request, response);
        }*/
        if (user != null) {
            request.getSession().setAttribute("user", user);
            return "redirect:admin/manage.html";
        }else {
            return "redirect:error.html";
        }
    }

}
